<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Check if there was an error on the most recent db operation performed</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongodb.getslaveokay.html">MongoDB::getSlaveOkay</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodb.listcollections.html">MongoDB::listCollections</a></div>
 <div class="up"><a href="class.mongodb.html">MongoDB</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongodb.lasterror" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB::lastError</h1>
  <p class="verinfo">(PECL mongo &gt;=0.9.5)</p><p class="refpurpose"><span class="refname">MongoDB::lastError</span> &mdash; <span class="dc-title">Check if there was an error on the most recent db operation performed</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongodb.lasterror-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span> <span class="methodname"><strong>MongoDB::lastError</strong></span>
    ( <span class="methodparam">void</span>
   )</div>

  <p class="para rdfs-comment">
   This method is equivalent to:
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">lastError</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">command</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'getlasterror'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mongodb.lasterror-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongodb.lasterror-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the error, if there was one.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mongodb.lasterror-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1396">
   <p><strong>Example #1 <span class="function"><strong>MongoDB::lastError()</strong></span> <strong><code>NULL</code></strong> error example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">resetError</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">lastError</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [&quot;err&quot;]=&gt;
  NULL
  [&quot;n&quot;]=&gt;
  int(0)
  [&quot;ok&quot;]=&gt;
  float(1)
}
</pre></div>
   </div>
  </div>
  <div class="example" id="example-1397">
   <p><strong>Example #2 <span class="function"><strong>MongoDB::lastError()</strong></span> duplicate key example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">selectCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;insert&nbsp;two&nbsp;documents&nbsp;with&nbsp;the&nbsp;same&nbsp;_id<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"_id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"_id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">lastError</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [&quot;err&quot;]=&gt;
  string(64) &quot;E11000 duplicate key errorindex: foo.foo.$_id_  dup key: { : 1 }&quot;
  [&quot;n&quot;]=&gt;
  int(0)
  [&quot;ok&quot;]=&gt;
  float(1)
}
</pre></div>
   </div>
  </div>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongodb.getslaveokay.html">MongoDB::getSlaveOkay</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodb.listcollections.html">MongoDB::listCollections</a></div>
 <div class="up"><a href="class.mongodb.html">MongoDB</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
